package com.thinktank.controller;

import java.io.*;
import java.sql.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import static com.thinktank.util.DbUtil.*;

import com.thinktank.vo.*;
import com.thinktank.dao.BoardDao;

public class BoardController implements Controller {
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = 1;
		String pg = request.getParameter("pg");
		if (pg != null)
			page = Integer.parseInt(pg);

		String field = request.getParameter("field");
		if (field == null)
			field = "BD_SUBJECT";

		String query = request.getParameter("query");
		if (query == null)
			query = "";
		else
			query = new String(query.getBytes("ISO-8859-1"), "UTF-8");

		HttpSession session = request.getSession();
		String icategory = null;
		icategory = request.getParameter("icategory");
		if (icategory == null) {
			icategory = (String) session.getAttribute("icategory");
		} else {
			session.setAttribute("icategory", icategory);

		}
		if (icategory == null)
			icategory = "0";
		// 방문 페이지 세션에 저장
		String url = null;
		if (request.getParameter("url") != null)
			url = request.getParameter("url");
		if (session.getAttribute("url") != null)
			url = (String)session.getAttribute("url");
		if (url != null)
			session.setAttribute("url", url);
		else {
			Member member = (Member) session.getAttribute("member");
			url = member.getmPage();
		}

		Connection con = getConnection();
		BoardDao dao = new BoardDao(con);
		List<Board> list = dao.getAllBoards(page, field, query, icategory, url);
		int count = dao.getCount(field, query, icategory);
		close(con);

		request.setAttribute("list", list);
		request.setAttribute("query", query);
		request.setAttribute("count", count);

		return "board.jsp";
	}
}